package org.example.util;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.serializer.SerializerFeature;

import java.util.Iterator;
import java.util.Map;
import java.util.TreeMap;

public class FastjsonSorter {

    public static void main(String[] args) {
        String json = "{\"code\":200,\"msg\":\"success\",\"data\":[{\"accountId\":\"459448595428\",\"appCode\":\"jdo-connect\",\"appGroup\":[\"PDCN\",\"VSD\",\"jdo-conenct\"]},{\"accountId\":\"150620102644\",\"appCode\":\"pos\",\"appGroup\":[\"PDCN\",\"DPP\",\"pos\"]},{\"accountId\":\"459448595428\",\"appCode\":\"digital-sales\",\"appGroup\":[\"PDCN\",\"DPP\",\"digital-sales\"]},{\"accountId\":\"839966665607\",\"appCode\":\"gravity-core\",\"appGroup\":[\"PDCN\",\"VSD\",\"gravity-core\"]},{\"accountId\":\"840138022652\",\"appCode\":\"connectivity\",\"appGroup\":[\"PDCN\",\"VSD\",\"connectivity\"]},{\"accountId\":\"150520991865\",\"appCode\":\"datalake\",\"appGroup\":[\"PDCN\",\"DPP\",\"datalake\"]},{\"accountId\":\"150620102644\",\"appCode\":\"smamo\",\"appGroup\":[\"PDCN\",\"DPP\",\"smamo\"]},{\"accountId\":\"459448595428\",\"appCode\":\"pmp\",\"appGroup\":[\"PDCN\",\"DPP\",\"pmp\"]},{\"accountId\":\"459448595428\",\"appCode\":\"iccpd-hgih\",\"appGroup\":[\"PDCN\",\"DPP\",\"iccpd-hgih\"]},{\"accountId\":\"150520991865\",\"appCode\":\"gio\",\"appGroup\":[\"PDCN\",\"DPP\",\"gio\"]},{\"accountId\":\"840138022652\",\"appCode\":\"gravity-core\",\"appGroup\":[\"PDCN\",\"VSD\",\"gravity-core\"]},{\"accountId\":\"150520991865\",\"appCode\":\"super-app\",\"appGroup\":[\"PDCN\",\"DPP\",\"super-app\"]},{\"accountId\":\"46765811-773a-4ed0-ba48-deefecbbbcf0\",\"appCode\":\"bcall\",\"appGroup\":[\"PDCN\",\"VSD\",\"bcall\"]},{\"accountId\":\"840138022652\",\"appCode\":\"my-porsche-app\",\"appGroup\":[\"PDCN\",\"VSD\",\"my-porsche-app\"]},{\"accountId\":\"459448595428\",\"appCode\":\"etmt\",\"appGroup\":[\"PDCN\",\"DPP\",\"etmt\"]},{\"accountId\":\"459448595428\",\"appCode\":\"datalake\",\"appGroup\":[\"PDCN\",\"DPP\",\"datalake\"]},{\"accountId\":\"150620102644\",\"appCode\":\"pec\",\"appGroup\":[\"PDCN\",\"DPP\",\"pec\"]},{\"accountId\":\"938210117248\",\"appCode\":\"myporsche\",\"appGroup\":[\"PDCN\",\"DPP\",\"myporsche\"]},{\"accountId\":\"722395152211\",\"appCode\":\"myporsche\",\"appGroup\":[\"PDCN\",\"DPP\",\"myporsche\"]},{\"accountId\":\"150520991865\",\"appCode\":\"digital-sales\",\"appGroup\":[\"PDCN\",\"DPP\",\"digital-sales\"]},{\"accountId\":\"840138022652\",\"appCode\":\"solid\",\"appGroup\":[\"PDCN\",\"VSD\",\"solid\"]},{\"accountId\":\"839966665607\",\"appCode\":\"jdo-connect\",\"appGroup\":[\"PDCN\",\"VSD\",\"jdo-conenct\"]},{\"accountId\":\"459448595428\",\"appCode\":\"myporsche\",\"appGroup\":[\"PDCN\",\"DPP\",\"myporsche\"]},{\"accountId\":\"609996596963\",\"appCode\":\"smamo\",\"appGroup\":[\"PDCN\",\"DPP\",\"smamo\"]},{\"accountId\":\"459448595428\",\"appCode\":\"fs\",\"appGroup\":[\"PDCN\",\"DPP\",\"fs\"]},{\"accountId\":\"839966665607\",\"appCode\":\"connectivity\",\"appGroup\":[\"PDCN\",\"VSD\",\"connectivity\"]},{\"accountId\":\"150620102644\",\"appCode\":\"gio\",\"appGroup\":[\"PDCN\",\"DPP\",\"gio\"]},{\"accountId\":\"32006581-0152-4c33-b259-8cae2eb9f2fc\",\"appCode\":\"bod\",\"appGroup\":[\"PDCN\",\"VSD\",\"bod\"]},{\"accountId\":\"839966665607\",\"appCode\":\"solid\",\"appGroup\":[\"PDCN\",\"VSD\",\"solid\"]},{\"accountId\":\"609996596963\",\"appCode\":\"jdo-connect\",\"appGroup\":[\"PDCN\",\"VSD\",\"jdo-conenct\"]},{\"accountId\":\"a0f5078c-bd6b-4826-b7a0-efb8cd2d6a72\",\"appCode\":\"bod\",\"appGroup\":[\"PDCN\",\"VSD\",\"bod\"]},{\"accountId\":\"459448595428\",\"appCode\":\"pec\",\"appGroup\":[\"PDCN\",\"DPP\",\"pec\"]},{\"accountId\":\"150620102644\",\"appCode\":\"etmt\",\"appGroup\":[\"PDCN\",\"DPP\",\"etmt\"]},{\"accountId\":\"150520991865\",\"appCode\":\"myporsche\",\"appGroup\":[\"PDCN\",\"DPP\",\"myporsche\"]},{\"accountId\":\"609996596963\",\"appCode\":\"connectivity\",\"appGroup\":[\"PDCN\",\"VSD\",\"connectivity\"]},{\"accountId\":\"150520991865\",\"appCode\":\"smamo\",\"appGroup\":[\"PDCN\",\"DPP\",\"smamo\"]},{\"accountId\":\"801719121160\",\"appCode\":\"pcds\",\"appGroup\":[\"PDCN\",\"VSD\",\"pcds\"]},{\"accountId\":\"840138022652\",\"appCode\":\"jdo-connect\",\"appGroup\":[\"PDCN\",\"VSD\",\"jdo-conenct\"]},{\"accountId\":\"459448595428\",\"appCode\":\"connectivity\",\"appGroup\":[\"PDCN\",\"VSD\",\"connectivity\"]},{\"accountId\":\"150520991865\",\"appCode\":\"etmt\",\"appGroup\":[\"PDCN\",\"DPP\",\"etmt\"]},{\"accountId\":\"150620102644\",\"appCode\":\"myporsche\",\"appGroup\":[\"PDCN\",\"DPP\",\"myporsche\"]},{\"accountId\":\"150520991865\",\"appCode\":\"pmp\",\"appGroup\":[\"PDCN\",\"DPP\",\"pmp\"]},{\"accountId\":\"609996596963\",\"appCode\":\"my-porsche-app\",\"appGroup\":[\"PDCN\",\"VSD\",\"my-porsche-app\"]},{\"accountId\":\"150520991865\",\"appCode\":\"pec\",\"appGroup\":[\"PDCN\",\"DPP\",\"pec\"]},{\"accountId\":\"150620102644\",\"appCode\":\"iccpd-hgih\",\"appGroup\":[\"PDCN\",\"DPP\",\"iccpd-hgih\"]},{\"accountId\":\"459448595428\",\"appCode\":\"smamo\",\"appGroup\":[\"PDCN\",\"DPP\",\"smamo\"]},{\"accountId\":\"150620102644\",\"appCode\":\"connectivity\",\"appGroup\":[\"PDCN\",\"VSD\",\"connectivity\"]},{\"accountId\":\"150620102644\",\"appCode\":\"digital-sales\",\"appGroup\":[\"PDCN\",\"DPP\",\"digital-sales\"]},{\"accountId\":\"801719121160\",\"appCode\":\"jdo-connect\",\"appGroup\":[\"PDCN\",\"VSD\",\"jdo-conenct\"]},{\"accountId\":\"150620102644\",\"appCode\":\"pmp\",\"appGroup\":[\"PDCN\",\"DPP\",\"pmp\"]},{\"accountId\":\"a0f5078c-bd6b-4826-b7a0-efb8cd2d6a72\",\"appCode\":\"bcall\",\"appGroup\":[\"PDCN\",\"VSD\",\"bcall\"]},{\"accountId\":\"801719121160\",\"appCode\":\"gravity-core\",\"appGroup\":[\"PDCN\",\"VSD\",\"gravity-core\"]},{\"accountId\":\"801719121160\",\"appCode\":\"connectivity\",\"appGroup\":[\"PDCN\",\"VSD\",\"connectivity\"]},{\"accountId\":\"46765811-773a-4ed0-ba48-deefecbbbcf0\",\"appCode\":\"bod\",\"appGroup\":[\"PDCN\",\"VSD\",\"bod\"]},{\"accountId\":\"801719121160\",\"appCode\":\"solid\",\"appGroup\":[\"PDCN\",\"VSD\",\"solid\"]},{\"accountId\":\"150520991865\",\"appCode\":\"wallbox\",\"appGroup\":[\"PDCN\",\"DPP\",\"wallbox\"]},{\"accountId\":\"150620102644\",\"appCode\":\"my-porsche-app\",\"appGroup\":[\"PDCN\",\"VSD\",\"my-porsche-app\"]},{\"accountId\":\"459448595428\",\"appCode\":\"pos\",\"appGroup\":[\"PDCN\",\"DPP\",\"pos\"]},{\"accountId\":\"46765811-773a-4ed0-ba48-deefecbbbcf0\",\"appCode\":\"infotainment\",\"appGroup\":[\"PDCN\",\"VSD\",\" infotainment backend\"]},{\"accountId\":\"a0f5078c-bd6b-4826-b7a0-efb8cd2d6a72\",\"appCode\":\"my-porsche-app\",\"appGroup\":[\"PDCN\",\"VSD\",\"my-porsche-app\"]},{\"accountId\":\"801719121160\",\"appCode\":\"my-porsche-app\",\"appGroup\":[\"PDCN\",\"VSD\",\"my-porsche-app\"]},{\"accountId\":\"609996596963\",\"appCode\":\"solid\",\"appGroup\":[\"PDCN\",\"VSD\",\"solid\"]},{\"accountId\":\"150620102644\",\"appCode\":\"super-app\",\"appGroup\":[\"PDCN\",\"DPP\",\"super-app\"]},{\"accountId\":\"609996596963\",\"appCode\":\"gravity-core\",\"appGroup\":[\"PDCN\",\"VSD\",\"gravity-core\"]},{\"accountId\":\"150620102644\",\"appCode\":\"datalake\",\"appGroup\":[\"PDCN\",\"DPP\",\"datalake\"]},{\"accountId\":\"459448595428\",\"appCode\":\"wallbox\",\"appGroup\":[\"PDCN\",\"DPP\",\"wallbox\"]},{\"accountId\":\"150520991865\",\"appCode\":\"fs\",\"appGroup\":[\"PDCN\",\"DPP\",\"fs\"]},{\"accountId\":\"459448595428\",\"appCode\":\"my-porsche-app\",\"appGroup\":[\"PDCN\",\"VSD\",\"my-porsche-app\"]},{\"accountId\":\"150620102644\",\"appCode\":\"wallbox\",\"appGroup\":[\"PDCN\",\"DPP\",\"wallbox\"]},{\"accountId\":\"150520991865\",\"appCode\":\"iccpd-hgih\",\"appGroup\":[\"PDCN\",\"DPP\",\"iccpd-hgih\"]},{\"accountId\":\"839966665607\",\"appCode\":\"my-porsche-app\",\"appGroup\":[\"PDCN\",\"VSD\",\"my-porsche-app\"]},{\"accountId\":\"a0f5078c-bd6b-4826-b7a0-efb8cd2d6a72\",\"appCode\":\"infotainment\",\"appGroup\":[\"PDCN\",\"VSD\",\" infotainment backend\"]},{\"accountId\":\"150520991865\",\"appCode\":\"pos\",\"appGroup\":[\"PDCN\",\"DPP\",\"pos\"]},{\"accountId\":\"459448595428\",\"appCode\":\"gio\",\"appGroup\":[\"PDCN\",\"DPP\",\"gio\"]},{\"accountId\":\"32006581-0152-4c33-b259-8cae2eb9f2fc\",\"appCode\":\"bcall\",\"appGroup\":[\"PDCN\",\"VSD\",\"bcall\"]},{\"accountId\":\"150620102644\",\"appCode\":\"fs\",\"appGroup\":[\"PDCN\",\"DPP\",\"fs\"]},{\"accountId\":\"459448595428\",\"appCode\":\"super-app\",\"appGroup\":[\"PDCN\",\"DPP\",\"super-app\"]},{\"accountId\":\"32006581-0152-4c33-b259-8cae2eb9f2fc\",\"appCode\":\"infotainment\",\"appGroup\":[\"PDCN\",\"VSD\",\" infotainment backend\"]}]}";

        String sortedJson = sortJson(json);
        System.out.println(sortedJson);
    }

    public static String sortJson(String json) {
        Object object = JSON.parse(json);
        Object sortedObject = sortObject(object);
        return JSON.toJSONString(sortedObject, SerializerFeature.PrettyFormat);
    }

    private static Object sortObject(Object object) {
        if (object instanceof JSONObject) {
            JSONObject jsonObject = (JSONObject) object;
            JSONObject sortedJsonObject = new JSONObject(new TreeMap<>());

            for (Map.Entry<String, Object> entry : jsonObject.entrySet()) {
                sortedJsonObject.put(entry.getKey(), sortObject(entry.getValue()));
            }

            return sortedJsonObject;
        } else if (object instanceof JSONArray) {
            JSONArray jsonArray = (JSONArray) object;
            JSONArray sortedJsonArray = new JSONArray();

            for (Object element : jsonArray) {
                sortedJsonArray.add(sortObject(element));
            }

            return sortedJsonArray;
        } else {
            return object;
        }
    }
}
